<?php
namespace Home\Controller;

use Utuo\WechatActivityController;
use My\Model\DistributionShopOrderModel;

class InviteController extends WechatActivityController
{
    
    // activity config
    private $activity;

    private $openIdKey = "";

    public function _initialize()
    {
        layout("Invite/layout");
        if (in_array(ACTION_NAME, array(
            'notifyInvite',
            'pay',
            'ajaxRegister'
        ))) {
            $this->setAuthEnabled(false);
        }
    }
    
    // 活动主页
    public function indexAction()
    {
        /* 获得inviterId */
        $cookieInviterIdKey = "inviter_id_{$this->cid}_1";
        $inviteId = I("inviterId", 0, "intval");
        if (! $inviteId) {
            $inviteId = cookie($cookieInviterIdKey) * 1;
        }
        
        /* 被邀请人是否打开处理 */
        /*
         * $url = "http://www.maka.im/k/rLhGa4";
         * $cookieOpenKey = "invite_url_open_{$this->cid}_1";
         * $opened = cookie($cookieOpenKey);
         * if ( !$opened ) { // 未打开
         * cookie($cookieOpenKey, 1);
         * $inviteId and cookie($cookieInviterIdKey, $inviteId);
         * redirect($url."?inviterId={$inviteId}");
         * exit;
         * } else { // 已打开
         * if ( !$_GET['inviterId'] ) { // 地址里无inviterId时
         * $inviteId and redirect($this->getCurrentUrl(array(
         * 'inviterId' => $inviteId,
         * )));
         * }
         * }
         */
        
        // 活动信息
        $this->activity = M('WechatActivityInvite')->where(array(
            'id' => $this->aid,
            'cid' => $this->cid
        ))->find() or $this->error("报名活动不存在");
        
        $this->activity['shareUrl'] = uu($this->cid,'Home','Invite','index',array('cid'=>$this->cid,'aid'=>$this->activity['id']),true);
        
        // 我的报名信息
        $this->rwSeparate(1);
        $registerInfo = M('WechatActivityInviteRegister')->where(array(
            'cid' => $this->cid,
            'aid' => $this->aid,
            'openId' => $this->openId
        ))->find();
        $this->rwSeparate(0);
        
        // 已经报名列表
        $registerList = M('WechatActivityInviteRegister')->where(array(
            'cid' => $this->cid,
            'aid' => $this->aid
        ))->select();
        
        $this->setShare(array(
            'title' => $this->activity['name'],
            'desc' => $this->activity['desc'],
            'imgUrl' => $this->activity['pic'],
            'link'=> $this->activity['shareUrl']
        ));
        
        // 视图
        $this->assign('userInfo', $this->userInfo);
        $this->assign('activity', $this->activity);
        $this->assign('registerInfo', $registerInfo);
        $this->assign('registerList', $registerList);
        $this->assign('payUrl', ($this->activity['fee'] * 1 ? "/index.php/Home/DisShopPay/pay?aid={$this->aid}&sid={$this->cid}" : ""));
        $this->assign('inviterId', $inviteId);
        $this->display();
    }
    
    // 用户报名
    public function ajaxRegisterAction()
    {
        $m = M('WechatActivityInviteRegister');
        
        // 活动信息
        $this->activity = M('WechatActivityInvite')->find($this->aid);
        
        // 检查是否已经登记
        $count = $m->where(array(
            'cid' => $this->cid,
            'aid' => $this->aid,
            'openId' => $this->openId
        ))->count();
        $count === false && $this->ajaxError('网络错误，请重试！(001)');
        $count > 0 && $this->ajaxError('您已经报名过了~' . $this->cid . "  " . $this->openId);
        
        C("TOKEN_ON", false);
        
        if ($m->create()) {
            $m->cid = $this->cid;
            $m->openId = $this->openId ? $this->openId : "test";
            $m->createTime = time();
            $m->orderNo = DistributionShopOrderModel::randOrderNo($this->cid);
            $data = $m->data();
            $data['id'] = $m->add();
            if ($data['id']) {
                $this->ajaxSuccess('报名成功，请您准时参加~');
            } else {
                $this->ajaxError('网络错误，请再试一次~');
            }
        } else {
            $this->ajaxError('提交数据错误，请检查重试~');
        }
    }
}